package com.lzyyj.gmis.base.webui.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lzyyj.gmis.base.model.IMenu;
import com.lzyyj.gmis.base.model.Menu;
import com.lzyyj.gmis.base.service.IMenuService;
import com.lzyyj.gmis.base.webui.ICurrentUserAware;

/**
 * 管理后台框架首页 
 * @author Lzyyj
 * @param <PK>
 */
@Scope("prototype")
@Controller
@SuppressWarnings("serial")
public class IndexAction<T, PK extends Serializable> extends BaseAction<IMenu<PK>,PK> implements SessionAware,RequestAware,ICurrentUserAware<T> {
	
	private static final Log log = LogFactory.getLog(IndexAction.class);
	
	protected Map<String, Object> session;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	protected Map<String, Object> request;
	
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	
	private T currentUser;

	@Override
	public void setCurrentUser(T currentUser) {
		this.currentUser = currentUser;
	}

	public IMenuService<PK> getMenuService() {
		return (IMenuService<PK>)baseService;
	}
	
	private List<IMenu<PK>> menus;
	
	public List<IMenu<PK>> getMenus() {
		return menus;
	}

	public void setMenus(List<IMenu<PK>> menus) {
		this.menus = menus;
	}
	
	/**
	 * 主窗口
	 */
	private String mainFrame;
	
	/**
	 * 系统标题
	 */
	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMainFrame() {
		return mainFrame;
	}

	public void setMainFrame(String mainFrame) {
		this.mainFrame = mainFrame;
	}
		
	/**
	 * 缺省入口
	 */
	@Override
	public String execute() throws Exception {
		return index();
	}

	/**
	 * 管理后台首页 index
	 * 访问或刷新管理后台首页
	 */
	public String index() {
		// 一般都会配置拦截器来过滤的，只有未配置拦截器时，才会为空
		if (currentUser==null){
			return LOGIN;
		}
		try {
			if(getMenuService()==null){
				menus = new ArrayList<IMenu<PK>>();
				IMenu<PK> menu = new Menu<PK>();
				menu.setName("测试管理");
				menus.add(menu);
				
				IMenu<PK> subMenu = new Menu<PK>();
				subMenu.setName("测试菜单1");
				menu.getMenus().add(subMenu);
				subMenu = new Menu<PK>();
				subMenu.setName("测试菜单2");
				menu.getMenus().add(subMenu);
				
				menu = new Menu<PK>();
				menu.setName("系统管理");
				menus.add(menu);
				subMenu = new Menu<PK>();
				
				subMenu.setName("系统菜单1");
				menu.getMenus().add(subMenu);
				subMenu = new Menu<PK>();
				subMenu.setName("系统菜单2");
				menu.getMenus().add(subMenu);
			}else {
				menus = getMenuService().getMenus();
			}
			session.put("menus", menus);
		} catch (Exception e) {
			this.addActionError("发送错误："+e);
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 顶部
	 * @return
	 */
	public String top(){
		return SUCCESS;
	}
	
	public String menu(){
		return SUCCESS;
	}

	public String main(){
		return SUCCESS;
	}
	
}